package zd.vrp.mop;

import java.util.ArrayList;
import java.util.List;

public class MultiObjective {
	private List objectives = null;
	public MultiObjective(){
		objectives = new ArrayList();
	}

	public List getObjectives() {
		return objectives;
	}

	public void setObjectives(List objectives) {
		this.objectives = objectives;
	}
	
	public int checkDomination(MultiObjective mo){		
		double[] d = new double[this.objectives.size()];
		int positiveNum = 0;
		int negativeNum = 0;
		int zeroNum = 0;
		for(int i=0;i<d.length;i++){
			double di = (Double)this.objectives.get(i);
			double mi = (Double)mo.getObjectives().get(i);
			if(di-mi<0){
				d[i] = -1;
				negativeNum ++;
			}else if(di-mi>0){
				d[i] = 1;
				positiveNum++;
			}else{
				d[i] = 0;
				zeroNum++;
			}
			
		}
		
		if(negativeNum > 0 && positiveNum == 0){
			return -1;
		}
		if(positiveNum > 0 && negativeNum == 0){
			return 1;
		}
		return 0;
	}
	
	public MultiObjectiveRank getRank(List<MultiObjective> moList){
		MultiObjectiveRank mor = new MultiObjectiveRank();
		
		int dominateNum = 0;
		int equalNum = 0;

		for(int i=0;i<moList.size();i++){
			if(this.checkDomination(moList.get(i))==-1){
				dominateNum++;
			}else if(this.checkDomination(moList.get(i))==0){
				equalNum++;
			}
		}
		
		mor.setDominateNum(dominateNum);
		mor.setEqualNum(equalNum);
			
		return mor;
	}
	
	public static void main(String[] args){
		MultiObjective mo1 = new MultiObjective();
		MultiObjective mo2 = new MultiObjective();
		MultiObjective mo3 = new MultiObjective();
		
		List l1 = new ArrayList();
		List l2 = new ArrayList();
		List l3 = new ArrayList();
		l1.add(1.0);
		l1.add(3.0);
		l2.add(1.0);
		l2.add(4.0);
		l3.add(1.0);
		l3.add(5.0);
		
		List<MultiObjective> moList = new ArrayList<MultiObjective>();
		
		mo1.setObjectives(l1);
		mo2.setObjectives(l2);
		mo3.setObjectives(l3);
		
		moList.add(mo1);
		moList.add(mo2);
		moList.add(mo3);
		
		System.out.println(mo1.checkUnodominated(moList));
		//System.out.println(mo1.checkDomination(mo3));
	}
	
	public boolean checkUnodominated(List<MultiObjective> moList){
		boolean result = true;
		
		for(int i=0;i<moList.size();i++){
			if(this.checkDomination(moList.get(i))==1){
				result = false;
				return result;
			}
		}
		
		return result;
	}
	
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		for(int i=0;i<this.objectives.size();i++){
			sb.append(this.objectives.get(i));
			sb.append(":");
		}
		sb.append("]");
		return sb.toString();
	}
}
